#include "stdafx.h"
#include "SpriteComponent.h"
#include "ResourceManager.h"
#include "SDLTexture.h"

namespace ZeEngine
{
	s64	SpriteComponent::m_type = -1;
	s64	SpriteComponent::m_index = -1;

    SpriteComponent::SpriteComponent(SDLTexture* pTexture) : m_pTexture(pTexture), m_initialized(false), IRenderableComponent("SpriteComponent", m_type, m_index)
    {
        //Default constructor set it to be the whole texture
        m_textureRect.x = 0;
        m_textureRect.y = 0;
        m_textureRect.h = 0;
        m_textureRect.w = 0;

        if (m_pTexture)
        {
            Vector2D<s32> textureSize = m_pTexture->GetDimension();

            m_textureRect.x = 0;
            m_textureRect.y = 0;
            m_textureRect.w = textureSize.GetX();
            m_textureRect.h = textureSize.GetY();

            m_initialized = true;
        }
    }

	SpriteComponent::SpriteComponent(SDLTexture* pTexture, const SDL_Rect& rect) : m_pTexture(pTexture), IRenderableComponent("SpriteComponent", m_type, m_index)
    {
        m_textureRect = rect;
        m_initialized = true;
    }

	SpriteComponent::SpriteComponent(ResourceManager& resourceManager, const std::string& textureName, const std::string& textureGroup) : SpriteComponent(resourceManager.GetResource<SDLTexture>(textureName, textureGroup))
	{}

	SpriteComponent::SpriteComponent(ResourceManager& resourceManager, const std::string& textureName, const std::string& textureGroup, const SDL_Rect& rect) : SpriteComponent(resourceManager.GetResource<SDLTexture>(textureName, textureGroup), rect)
	{}

    SpriteComponent::~SpriteComponent()
    {
    }

    const SDL_Rect& SpriteComponent::GetTextureRect()     const
    {
        if (!m_initialized)
        {
            if (m_pTexture && m_pTexture->IsLoaded())
            {
                Vector2D<s32> textureSize = m_pTexture->GetDimension();

                m_textureRect.x = 0;
                m_textureRect.y = 0;
                m_textureRect.w = textureSize.GetX();
                m_textureRect.h = textureSize.GetY();
                m_initialized = true;
            }
        }

        return m_textureRect;
    }

    void SpriteComponent::Render(SDL_Renderer& renderer, const Vector2D<float> position, const f32 rotation, f32 interpolation)
	{
		SDLTexture* pSDLTexture = GetTexture();

		if (pSDLTexture)
		{
			SDL_Texture* pTexture = pSDLTexture->GetTexture();

			SDL_Rect dest = GetTextureRect();
			dest.x = static_cast<int>(position.GetX());
			dest.y = static_cast<int>(position.GetY());

			if (pTexture)
			{
				//const s32 flipX = GetFlip().GetX() ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
				//const s32 flipY = GetFlip().GetY() ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
				const s32 flipX = SDL_FLIP_NONE;
				const s32 flipY = SDL_FLIP_NONE;

				SDL_RenderCopyEx(&renderer, pTexture, &GetTextureRect(), &dest, rotation, NULL, static_cast<SDL_RendererFlip>(flipX | flipY));
			}
		}
	}
}
